<?php
require_once('inc/init.php');
require_once('inc/pager.php');
require_once getCacheFilePath('forums.php');
require_once getCacheFilePath('stamps.php');
require_once('inc/editor/ubb.php');

if(!isLogin()){
	_header_("location:login.php?ref=".urlencode($_SERVER["REQUEST_URI"]) );
}

$fid=numFilter($_GET['fid']);
$userpass=strFilter($_POST['userpass']);
if(!empty($userpass)){
	if(encrypt($lg['username'],$userpass)==$lg['userpass']){
		$_SESSION['isadmin'] = 1;
		_header_("location:admin.php?fid={$fid}");

	}
}

if(empty($lg['isadmin'])){
	printMsg('forum_adminpass');
}

$headtitle="管理面板";
$orderby=strFilter($_GET['orderby']);
$d=numFilter($_GET['d']);
$action=strFilter($_GET['action']);


if(!isForumAdmin($fid)){
	exit();
}


$topicverify_count = $db->row_select_one("topics","fid={$fid} and verifystate=1 and isrecycle=0","count(0) as allnum");
$postverify_count = $db->row_select_one("posts","posts.verifystate=1 and posts.fid={$fid} and posts.istopic=0","count(0) as allnum");

$includepath='';
$guidemenu="";
$guidemenu.="<p><a href=\"admin.php?action=topic&fid={$fid}\">主题管理</a></p>";
$guidemenu.="<p><a href=\"admin.php?action=topicverify&fid={$fid}\">主题审核</a> ({$topicverify_count['allnum']})</p>";
$guidemenu.="<p><a href=\"admin.php?action=postverify&fid={$fid}\">回复审核</a> ({$postverify_count['allnum']})</p>";
$guidemenu.="<p><a href=\"admin.php?action=recycle&fid={$fid}\">主题回收站 </a></p>";
$guidemenu.="<p><a href=\"admin.php?action=announces&fid={$fid}\">版块公告</a></p>";
$guidemenu.="<hr />";
$guidemenu.="<p><a href=\"admin.php?action=exit&fid={$fid}\">退出管理</a></p>";
$actiontitle='<span>管理面板</span>  » ';

switch($action){
	case "":
	case "topic":
		$actiontitle.='<span>主题管理</span>';
		$curPage = numFilter($_GET["page"]);
		$forum=$db->row_select_one("forums","id={$fid}");
		$cache_forum = $cache_forums[$fid];
		
		$pager = new Pager();
		$pager->init(10,$curPage,"admin.php?action={$action}&fid={$fid}&page={page}");
		$condition="(fid={$fid} and isrecycle=0 and verifystate=0)";
		switch($orderby){
			case "replynum":
				$orderstr="replynum desc";
			break;
			case "hits":
				$orderstr="hits desc";
			break;
			case "author":
				$orderstr="userid";
			break;
			case "posttime":
				$orderstr="posttime desc";
			break;
			case "lastreply":
				$timed = time()-$d;
				$condition.=" and ordertime>{$timed}";
				$orderstr="ordertime desc";
			break;
			case "isgood":
				$orderstr="isgood desc";
			break;
			default:
				$orderstr="isalltop desc,istop desc, ordertime desc";
			break;
		}
			
		$rows = $pager->queryRows($db,"topics", $condition, "*",$orderstr);
		foreach($rows as $key=>$row){
			$row['posttime']=getDateStr($row['posttime']);
			$row['title']=htmlFilter($row['title']);
			$row['username']=htmlFilter($row['username']);
			if(!empty($row['highlight'])){
				$row['title']=genHighLighStr($row['highlight'],$row['title']);
			}
			$lastre=explode("\t",$row['lastreply']);
			$row['rowuser']=htmlFilter($lastre[1]);
			$row['rowlasttime'] = getDateStr($lastre[0]);
			$row['icon']=getTopicIcon($row);
			$rows[$key]=$row;
		}
		$includepath='admin_topic.htm';
	break;

	case "topicverify":
		$actiontitle.='<span>主题审核</span>';
		$curPage = numFilter($_GET["page"]);
		$pager = new Pager();
		$pager->init(10,$curPage,"admin.php?action={$action}&fid={$fid}&page={page}");
		$rows = $pager->queryRows($db,"topics", "fid={$fid} and verifystate=1 and isrecycle=0","*","id desc");
		foreach($rows as $key=>$row){
			$row['posttime']=getDateStr($row['posttime']);
			$row['title']=htmlFilter($row['title']);
			$row['username']=htmlFilter($row['username']);
			$row['icon']=getTopicIcon($row);
			$rows[$key]=$row;
		}
		
		$includepath="admin_{$action}.htm";
	break;


	case "recycle":
		$actiontitle.='<span>主题回收站</span>';
		$curPage = numFilter($_GET["page"]);
		$pager = new Pager();
		$pager->init(10,$curPage,"admin.php?action={$action}&fid={$fid}&page={page}");
		$rows = $pager->queryRows($db,"topics", "fid={$fid} and verifystate=0 and isrecycle=1" , "*","id desc");
		foreach($rows as $key=>$row){
			$row['posttime']=getDateStr($row['posttime']);
			$row['title']=htmlFilter($row['title']);
			$row['username']=htmlFilter($row['username']);
			$lastre=explode("\t",$row['lastreply']);
			$row['rowuser']=htmlFilter($lastre[1]);
			$row['rowlasttime']=getDateStr($lastre[0]);
			$row['icon']=getTopicIcon($row);
			$rows[$key]=$row;
		}
		$includepath="admin_{$action}.htm";
	break;

	case "postverify":
		$actiontitle.='<span>回复审核</span>';
		$curPage = numFilter($_GET["page"]);
		$pager = new Pager();
		$pager->init(10,$curPage,"admin.php?action={$action}&fid={$fid}&page={page}");
		$rows = $pager->queryRows($db,"posts,topics", "posts.verifystate=1 and posts.fid={$fid} and posts.istopic=0 and posts.tid=topics.id" ,"*,topics.title as title,posts.id as id,posts.username as username, posts.userid as userid, posts.posttime as posttime","posts.id desc");
		foreach($rows as $key=>$row){
			$row['posttime']=getDateStr($row['posttime']);
			$row['title']=htmlFilter($row['title']);
			$row['username']=htmlFilter($row['username']);
			$row['content']=$ubb->ubb2html($row['content']);
			$rows[$key]=$row;
		}
		$includepath="admin_{$action}.htm";
	break;

	case "announces":
		$ancid=numFilter($_GET['ancid']);
		if(empty($ancid)){
			$rows = $db->row_select("announces","fid={$fid}", 0 ,"id,username,title,starttime,stoptime,posttime,type,url,color,ordernum", "ordernum, id desc");
			foreach($rows as $key=>$row){
				$row['posttime']=getDateStr($row['posttime']);
				$row['starttime']=empty($row['starttime'])?"无限制":getDateStr($row['starttime'],'dateonly',false);
				$row['stoptime']=empty($row['stoptime'])?"无限制":getDateStr($row['stoptime'],'dateonly',false);
				$row['title']=htmlFilter($row['title']);
				$row['username']=htmlFilter($row['username']);
				$row['rowtype'] = numFilter($row['type'])==1?'链接':'文本';
				$row['rowlink'] = numFilter($row['type'])==1? $row['url']:"announce.php?id={$row[id]}";
				$rows[$key]=$row;
			}
		}else{
			$ancrow=$db->row_select_one("announces","id={$ancid}");
			$ancrow['starttime']=empty($ancrow['starttime'])?"":getDateStr($ancrow['starttime'],'dateonly',false);
			$ancrow['stoptime']=empty($ancrow['stoptime'])?"":getDateStr($ancrow['stoptime'],'dateonly',false);
		}
		$pagetitle = empty($ancid)?"发布公告":"编辑公告";
		$actiontitle .= "<span>{$pagetitle}</span>";
		$includepath="admin_{$action}.htm";
	break;

	case "exit":
		$_SESSION['isadmin']='';
		_header_("location:forum.php?fid={$fid}");
	break;

	default:
		exit();
	break;

}
require_once('header.php');
require_once getTemplatePath('admin.htm');
footer();
?>